package wec_wallet.ebo.com.activity.options;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import bassproject.ebo.com.ebobass.activity.BassActivityController;
import bassproject.ebo.com.ebobass.activity.MyBaseActivity;
import bassproject.ebo.com.ebobass.utils.I_Share;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemClick;
import wec_wallet.ebo.com.R;
import wec_wallet.ebo.com.activity.MineActivity;
import wec_wallet.ebo.com.activity.main.MainActivity;
import wec_wallet.ebo.com.bean.LanguageBean;

public class LanguageSetActivity extends MyBaseActivity {

    @BindView(R.id.actionbar_back)
    RelativeLayout actionbar_back;

    @BindView(R.id.lv_language)
    ListView lv_language;
    LanguageAdapter languageAdapter;
    ArrayList<LanguageBean> listLanguageBeans;

    private int prevLangugeType;//原來的语言类型
    private int curLangugeType;//当前选中的语言类型

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_language_set);
        ButterKnife.bind(this);
        prevLangugeType = I_Share.getLanguageType();
        initViews();
    }

    private String getLanguageName(String str){
        return str.substring(0,str.lastIndexOf("("));
    }

    @Override
    public void initViews() {
        listLanguageBeans = new ArrayList<LanguageBean>();
        addLanguage(getLanguageName(getResources().getString(R.string.lan_zh_cn)), false);
        addLanguage(getLanguageName(getResources().getString(R.string.lan_zh_tw)), false);
        addLanguage(getLanguageName(getResources().getString(R.string.lan_en_us)), false);
//        addLanguage(getLanguageName(getResources().getString(R.string.lan_ja_jp)), false);
        for(int i=0;i<listLanguageBeans.size();i++){
            if(i==prevLangugeType){
                listLanguageBeans.get(i).setSelected(true);
            }
        }
        languageAdapter = new LanguageAdapter(LanguageSetActivity.this, listLanguageBeans);
        lv_language.setAdapter(languageAdapter);
    }

    private void addLanguage(String name,boolean selected){
        LanguageBean languageBean;
        languageBean = new LanguageBean(name, selected);
        listLanguageBeans.add(languageBean);
    }

    @Override
    public void onClick(View v) {

    }

    @OnItemClick(R.id.lv_language)
    void clickLvLanguage(int position) {
//        Lg.d("clickLvLanguage position =" + position);
        languageAdapter.select(position);
        curLangugeType = position;

    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onClick_Back();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }



    //返回键
    @OnClick(R.id.actionbar_back)
    void onClick_Back() {
        if(prevLangugeType!=curLangugeType){
            //离开界面时，如果语言选项发生改变，则执行切换语言逻辑
            I_Share.setLanguageType(curLangugeType);
        }
        BassActivityController.removeALLActivity();
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
        finish();
    }


//adapter==========================================================================================================================

    private static class ViewHolder {
        TextView tv_language;
        CheckBox cb_selector;
    }

    public class LanguageAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        private ArrayList<LanguageBean> data;

        public LanguageAdapter(Context context, ArrayList<LanguageBean> _data) {
            this.inflater = LayoutInflater.from(context);
            data = _data;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.item_language, null);
                holder.tv_language = (TextView) convertView.findViewById(R.id.tv_language);
                holder.cb_selector = (CheckBox) convertView.findViewById(R.id.cb_selector);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            LanguageBean languageBean = (LanguageBean) getItem(position);
            holder.tv_language.setText(languageBean.getLanguage());
            holder.cb_selector.setChecked(languageBean.isSelected());
            return convertView;
        }

        public void select(int position) {
            for (int i = 0; i < data.size(); i++) {
                data.get(i).setSelected(false);
            }
            data.get(position).setSelected(true);
            notifyDataSetChanged();
        }
    }


}
